package objects.cicleItem
{
	import flash.events.Event;
	import flash.geom.Point;
	import starling.core.Starling;
	import starling.display.Image;
	import starling.display.MovieClip;
	import starling.display.Sprite;
	import starling.events.Event;
	import starling.events.Touch;
	import starling.events.TouchEvent;
	import starling.events.TouchPhase;
	import starling.filters.BlurFilter;
	import starling.filters.ColorMatrixFilter;
	import starling.filters.FragmentFilter
	import com.greensock.TweenLite;
	import com.greensock.*;
	import com.greensock.easing.*;
	
	/**
	 * ...
	 * @author
	 */
	public class cicleItemBaseBuild extends Cicle_Item
	{
		private var m_percentCoin:Number;
		
		public function cicleItemBaseBuild(_sprite:Sprite, _pos:Point)
		{
			
			super(_sprite, _pos);
			
			this.addEventListener(starling.events.Event.ADDED_TO_STAGE, init);
			m_percentCoin = Number(database.instance.RESULTCANONARRAY[7]);
			m_percentCoin = m_percentCoin / 100;
			_sprite.addChild(this);
		}
		
		override public function update(dt:Number):void
		{
		}
		
		public function setCicleInfo():void
		{
			m_baseItem[0].m_coint = 70;
			m_baseItem[1].m_coint = 70;
			m_baseItem[2].m_coint = 125 * m_percentCoin;
			m_baseItem[3].m_coint = 0;
			setItemInfo();
		}
		
		public function setItemInfo():void
		{
			m_baseItem[0].refresh();
			m_baseItem[0].addText("  Nhà cung thủ\n\n\n\n                          4-6\n                          Trung bình\n                          " + m_baseItem[0].m_coint, "0xffffff", 15, 3);
			m_baseItem[0].addText("  Bắn  tên giết địch với sự \n  chính xác cao\n\n  Sức công phá:\n  Tốc độ bắn:\n  Giá:", "0xFFFF00", 14, 25);
			m_baseItem[0].setScale(90,70);
			
			
			if (Number(database.instance.RESULTSOLIDERARRAY[3]) == 0)
			{
				m_baseItem[1].refresh();
				m_baseItem[1].addText("  Nhà lính\n\n\n\n\n                         Không giáp\n                          1-3\n                          " + database.instance.RESULTSOLIDERARRAY[0] + "\n                          " + 125 * m_percentCoin, "0xffffff", 15, 3);
				m_baseItem[1].addText("  Luyện quân lính có khả năng\n  chặn và giết địch và rất\n  trung thành\n\n  Phòng thủ:\n  Tấn công:\n  Sinh lực:\n  Giá:", "0xFFFF00", 14, 25);
				m_baseItem[1].setScale(129,110);
			}
			else if (Number(database.instance.RESULTSOLIDERARRAY[3]) == 1)
			{
				m_baseItem[1].refresh();
				m_baseItem[1].addText("  Nhà lính\n\n\n\n\n                         Giáp thấp\n                          1-3\n                          " + database.instance.RESULTSOLIDERARRAY[0] + "\n                          " + 125 * m_percentCoin, "0xffffff", 15, 3);
				m_baseItem[1].addText("  Luyện quân lính có khả năng\n  chặn và giết địch và rất\n  trung thành\n\n  Phòng thủ:\n  Tấn công:\n  Sinh lực:\n  Giá:", "0xFFFF00", 14, 25);
				m_baseItem[1].setScale(129,110);
			}
			m_baseItem[2].refresh();
			m_baseItem[2].addText("  Nhà Pháo\n\n\n\n\n                             " + database.instance.RESULTCANONARRAY[0] + "-" + database.instance.RESULTCANONARRAY[0] * 2 + "\n                             " + m_baseItem[2].m_coint , "0xffffff", 15, 3);
			m_baseItem[2].addText("  Bắn ra đạn pháo với sức\n  công phá lớn và trong\n  diện rộng \n\n  Sức công phá:\n  Giá:", "0xFFFF00", 14, 25);
			m_baseItem[2].setScale(130,70);
			
			m_baseItem[3].refresh();
			m_baseItem[3].addText("  Khóa\n", "0xffffff", 15, 5);
			m_baseItem[3].setScale(-10,-10);
		}
		
		private function init(e:starling.events.Event):void
		{
			removeEventListener(starling.events.Event.ADDED_TO_STAGE, init);
			//vong tron
			m_spriteMovie = new MovieClip(Assets.getSpriteSheet().getTextures("BASE_BUILD_SELECT"), 1);
			m_spriteMovie.width = 150;
			m_spriteMovie.height = 150;
			this.addChild(m_spriteMovie);
			m_spriteMovie.alpha = 0;
			
			//item
			m_baseItem = new Array();
			var _item:itemInCicle = new itemInCicle(150, "icon_" + 1, m_sprite);
			_item.pivotX = _item.width / 2;
			_item.pivotY = _item.height / 2;
			_item.alpha = 0;
			m_baseItem.push(_item);
			this.addChild(_item);
			
			_item = new itemInCicle(150, "icon_" + 2, m_sprite);
			_item.pivotX = _item.width / 2;
			_item.pivotY = _item.height / 2;
			_item.alpha = 0;
			m_baseItem.push(_item);
			this.addChild(_item);
			
			_item = new itemInCicle(150, "icon_" + 4, m_sprite);
			_item.pivotX = _item.width / 2;
			_item.pivotY = _item.height / 2;
			_item.alpha = 0;
			m_baseItem.push(_item);
			this.addChild(_item);
			
			_item = new itemInCicle(150, "icon_" + 3, m_sprite);
			_item.pivotX = _item.width / 2;
			_item.pivotY = _item.height / 2;
			_item.alpha = 0;
			m_baseItem.push(_item);
			this.addChild(_item);
			
			this.setAnchorPoint();
			this.setPos(m_pos);
			this.setCicleInfo();
		}
		
		override public function clockItem(_index:int):void
		{
			this.removeChild(m_baseItem[_index]);
			//m_baseItem[_index]=new itemInCicle(150, "icon_4", m_sprite);
			m_baseItem[_index].filter = null;
			this.addChild(m_baseItem[_index]);
			
			this.setAnchorPoint();
			this.setPos(m_pos);
			this.setCicleInfo();
		}
		
		override public function unClockItem(_index:int):void
		{
			this.removeChild(m_baseItem[_index]);
			m_baseItem[_index].filter = null;
			m_baseItem[_index].alpha = 0;
			this.addChild(m_baseItem[_index]);
			
			this.setAnchorPoint();
			this.setPos(m_pos);
			this.setCicleInfo();
		}
		
		override public function clockAndUnclock():void
		{
			for (var i:int = 0; i < m_baseItem.length; i++)
			{
				if (m_baseItem[i].m_enabled == false)
				{
					clockItem(i);
					colorMatrixFilter = new ColorMatrixFilter();
					colorMatrixFilter.adjustSaturation(-1); // make image Grayscale
					m_baseItem[i].filter = colorMatrixFilter;
				}
				else
				{
					unClockItem(i);
					colorMatrixFilter = new ColorMatrixFilter();
					colorMatrixFilter.adjustSaturation(0); // make image Grayscale
					m_baseItem[i].filter = colorMatrixFilter;
				}
			}
		}
		
		public function setPos(_pos:Point):void
		{
			m_spriteMovie.x = _pos.x - 20;
			m_spriteMovie.y = _pos.y - 20;
			
			m_baseItem[3].x = _pos.x + m_spriteMovie.width / 2 - m_baseItem[3].width / 2;
			m_baseItem[3].y = _pos.y - m_spriteMovie.height / 2 + m_baseItem[3].height / 2;
			
			m_baseItem[2].x = _pos.x - m_spriteMovie.width / 2 + m_baseItem[2].width / 2;
			m_baseItem[2].y = _pos.y + m_spriteMovie.height / 2 - m_baseItem[2].height / 2 + m_baseItem[2].m_textY;
			
			m_baseItem[1].x = _pos.x + m_spriteMovie.width / 2 - m_baseItem[1].width / 2;
			m_baseItem[1].y = _pos.y + m_spriteMovie.height / 2 - m_baseItem[1].height / 2 + m_baseItem[1].m_textY;
			
			m_baseItem[0].x = _pos.x - m_spriteMovie.width / 2 + m_baseItem[0].width / 2;
			m_baseItem[0].y = _pos.y - m_spriteMovie.height / 2 + m_baseItem[0].height / 2;
			
			var x0:int = m_baseItem[0].x;
			var y0:int = m_baseItem[0].y;
			var x1:int = m_baseItem[1].x;
			var y1:int = m_baseItem[1].y;
			var x2:int = m_baseItem[2].x;
			var y2:int = m_baseItem[2].y;
			var x3:int = m_baseItem[3].x;
			var y3:int = m_baseItem[3].y;
			
			m_baseItem[0].m_info.x = x0 + m_baseItem[0].width;
			m_baseItem[0].m_info.y = y0 - m_baseItem[0].height;
			
			m_baseItem[1].m_info.x = x1 + m_baseItem[1].width;
			m_baseItem[1].m_info.y = y1 - m_baseItem[1].height;
			
			m_baseItem[2].m_info.x = x2 + m_baseItem[2].width;
			m_baseItem[2].m_info.y = y2 - m_baseItem[2].height;
			
			m_baseItem[3].m_info.x = x3 + m_baseItem[3].width;
			m_baseItem[3].m_info.y = y3 - m_baseItem[3].height;
		
		}
		
		override public function release():void
		{
			for (var i:int = 0; i < m_baseItem.length; i++)
			{
				m_baseItem[i].release();
			}
			this.removeChildren();
			m_sprite.removeChild(this);
		}
	}

}